<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use DB ;
use App\Http\Requests;
use App\Http\Controllers\Controller;

use Hash;
class UserController extends Controller
{
    //add
    public function add()
    {
    	return view('admin.user.add',['title'=> '用户添加']);
    }
    public function insert(Request $request)
    {	
    	$this -> validate($request,[
    	'name' => 'required|min:6|max:18',
    	'email' => 'required|email',
    	'password' => 'required',
    	'repassword' => 'required|same:password'
    	],[
        'name.required' => '用户名不能为空',
        'name.min' => '用户名最少六位',
        'name.max' => '用户名最多十八位',
        'email.required' => '邮箱不能为空',
        'email.email' => '邮箱格式不正确',
        'password.required' => '密码不能为空',
        'repassword.required' => '确认密码不能为空',
        'repassword.same' => '确认密码不一致'
    ]);

    	$data = $request -> except('_token','repassword');
    	$data['password'] = Hash::make($data['password']);
    	$data['status'] = 0;
    	$data['remember_token'] = str_random(50);

        $time = time();
        $data['created_at'] = $time;
        $data['updated_at'] = $time;

    	if($request -> hasfile('pic'))
    	{
    		if($request -> file('pic') -> isValid())
    		{
    			$suffix = $request -> file('pic') -> getClientOriginalExtension();
    			$fileName = time().mt_rand(10000,90000). '.' . $suffix;
                $move = $request -> file('pic') -> move('./uploads/yhtx',$fileName);
                if($move)
                {
                    $data['pic'] = $fileName;
                }else
                {
                    $data['pic'] = 'default.jpg';
                }
    		}else
                {
                    $data['pic'] = 'default.jpg';
                }
    	}else
            {
                $data['pic'] = 'default.jpg';
           }
           $res = DB::table('users') -> insert($data);
           if($res)
           {
                return redirect('/admin/user/index')->with(['info' => '添加成功']);
           }else
           {
                return back() -> with(['info' => '添加失败']);
           }
    }
    //用户列表页
    public function index(Request $request)
    {
        //搜索        

        //查询数据   分页  paginate
        $data = DB::table('users') ->where('name','like','%'.$request->input('keywords').'%')->paginate($request -> input('num',10));
        return view('admin.user.index',['data' => $data,'request' => $request -> all()]);
    }

    //ajax修改用户状态
    public function ajaxStatus(Request $request)
    {
        $id = $request -> input('id');
        $status = DB::table('users') -> where('id',$id) -> first() -> status;

        //判断
        if($status == 0)
        {
           $res =  DB::table('users') -> where('id',$id) -> update(['status' => 1]);
           if($res)
           {
                return 1;
           }else
           {
                return 2;
           }
        }elseif($status ==1)
        {   
            $res = DB::table('users') -> where('id',$id) -> update(['status' => 0]);
            if($res)
            {
                return 0;
            }else
            {
                return 2;
            }
        }
    } 
    public function ajaxName(Request $request)
    {
        $id = $request -> id;
        $name = $request -> name;

        $data = DB::table('users') -> where('name',$name) -> first();

        if($data)
        {
            return 4;
        }else
        {
            $res = DB::table('users') -> where('id',$id) -> update(['name'  => $name]);
            if($res)
            {
                return 1;
            }else
            {
                return 0;
            }
        }
    }  
    public function edit($id)
    {
        $data = DB::table('users') -> where('id',$id) -> first();

        return view('admin.user.edit',['data' => $data,'title' => '用户编辑']);
    }
    public function update(Request $request)
    {
        $data = $request -> except('_token','id');

        $oldPic = DB::table('users') -> where('id',$request->id)->first()->pic;

        if($request -> hasfile('pic'))
        {
            if($request -> file('pic')->isValid())
            {
                $suffix = $request -> file('pic') -> getClientOriginalExtension();
                $fileName = time().mt_rand(10000,90000). '.' . $suffix;
                $move = $request -> file('pic') -> move('./uploads/yhtx',$fileName);
                if($move)
                {
                    $data['pic'] = $fileName;
                    if($oldPic != 'default.jpg'){   
                        //删除原头像
                        unlink("./uploads/yhtx/".$oldPic);
                    }
                }
            }   
        }

        //执行修改
        $res = DB::table('users') ->where('id',$request->id)->update($data);
         if($res)
           {
                return redirect('/admin/user/index')->with(['info' => '修改成功']) ;
           }else
           {
                return back()->with(['info' => '修改失败']);
           }

    } 
    public function delete($id)
    {
        $oldPic = DB::table('users') -> where('id',$id)->first()->pic;
        $res = DB::table('users')  -> delete($id);
        if($res)
           {    
            if(file_exists('./uploads/yhtx/'.$oldPic))
            {   
                    if($oldPic != 'default.jpg')
                    {
                        unlink('./uploads/yhtx/'.$oldPic);
                    }
            }
               return redirect('/admin/user/index')->with(['info' => '删除成功']) ;
           }else
           {
                return back()->with(['info' => '删除失败']);
           }

    }
}
